package pl.edu.pw.perg.compinfodb.datamodel;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import pl.edu.pw.perg.compinfodb.entities.Attribute;
import pl.edu.pw.perg.compinfodb.entities.PhysicalElement;

public class TableData {

	public static final String SLOT = "SLOT";
	public static final String LOCATION = "LOCATION";
	public static final String LABEL = "LABEL";
	public static final String ELEMENT_TYPE = "ELEMENT_TYPE";
	public static final String FROM = "FROM";
	public static final String TO = "TO";
	public static final String FROM_C = "FC#";
	public static final String TO_C = "TC#";
	
	private Map<String, String> values = new HashMap<String, String>();
	private PhysicalElement pe;

	public Object getValue(String fieldName) {
		return values.get(fieldName);

	}

	public void setValue(String fieldName, String value) {
		if(fieldName.equals(LABEL))
			pe.setLabel(value);
		else
			pe.setAttribute(fieldName, value);
		values.put(fieldName, value);
		
	}

	public boolean matchesFilter(Map<String, String> filter) {
		if(filter == null)
			return true;
		for (String key : filter.keySet()) {
			if(getValue(key) == null)
				continue;
			String value = getValue(key).toString();
			if (!value.contains(filter.get(key))) {
				return false;
			}
		}
		return true;
	}

	public TableData(PhysicalElement pe) {
		this.pe = pe;
		values.put(ELEMENT_TYPE, pe.getElementType().getTypeName());
		values.put(LABEL, pe.getLabel());
		values.put(FROM, pe.getInputElementName());
		values.put(TO, pe.getOutputElementName());
		values.put(FROM_C, (pe.getInputElementName() == null) ? "" :pe.getInputElementConn());
		values.put(TO_C, (pe.getOutputElementName() == null) ? "" :pe.getOutputElementConn());
		 values.put(LOCATION, (pe.getVirtualElement() == null) ? "" : pe.getVirtualElement().toString());
		 values.put(SLOT, (pe.getVirtualElement() != null && pe.getVirtualElement().getLocation()!=-1)?
		 "" + pe.getVirtualElement().getLocation() : "N/A");
		Collection<Attribute> attributes = pe.getAttributes();
		for (Attribute attribute : attributes) {
			values.put(attribute.getField().getFieldName(), attribute.getValue());
		}

	}

//	public static String[] getConstHeaders() {
//		String[] ret = { LABEL, ELEMENT_TYPE, LOCATION, SLOT };
//		return ret;
//	}

	public PhysicalElement getPe() {
		return pe;
	}
	
	public boolean validate(){
		return pe.getLabel() != null;
	}

}
